local hx__qixi = fk.CreateSkill {
  name = "hx__qixi",
}

hx__qixi:addEffect("active", {
  anim_type = "offensive",
  prompt = "#hx__qixi",
  can_use = function(self, player)
    return player:getMark("@hx__qixi") > 0
  end,
  card_num = 0,
  target_num = 0,
  on_use = function(self, room, effect)
    local player = effect.from
    local choice = room:askToChoice(player, {
      choices = {"hx__qixi_get", "hx__qixi_discard"},
      skill_name = hx__qixi.name,
    })
    local tar = table.filter(room:getAlivePlayers(), function(p) return not p:isAllNude() end)
    room:removePlayerMark(player, "@hx__qixi", 1)
    if choice == "hx__qixi_get" then
      local targets = room:askToChoosePlayers(player, {
        targets = tar,
        min_num = 1,
        max_num = 1,
        prompt = "#hx__qixi_get_target",
        skill_name = hx__qixi.name,
        cancelable = false,
      })
      if #targets > 0 then
        local card = room:askToChooseCard(player,{
            target = targets[1],
            flag = "hej",
            prompt = "#hx__qixi_card",
            skill_name = hx__qixi.name,
        })
        room:moveCardTo(card, Player.Hand, player, fk.ReasonJustMove, hx__qixi.name, "", true, player)
      end
    elseif choice == "hx__qixi_discard" then
      local tar1 = room:askToChoosePlayers(player, {
        targets = tar,
        min_num = 1,
        max_num = 1,
        prompt = "#hx__qixi_get_target",
        skill_name = hx__qixi.name,
        cancelable = true,
      })
      if #tar1 > 0 then
        local id1 = room:askToChooseCard(player,{
          target = tar1[1],
          flag = "hej",
          prompt = "#hx__qixi_card",
          skill_name = hx__qixi.name,
        })
        room:throwCard({id1}, hx__qixi.name, tar1[1], player)
      end
      local tar2 = room:askToChoosePlayers(player, {
        targets = tar,
        min_num = 1,
        max_num = 1,
        prompt = "#hx__qixi_get_target",
        skill_name = hx__qixi.name,
        cancelable = true,
      })
      if #tar2 > 0 then
        local id2 = room:askToChooseCard(player,{
          target = tar1[1],
          flag = "hej",
          prompt = "#hx__qixi_card",
          skill_name = hx__qixi.name,
        })
        room:throwCard({id2}, hx__qixi.name, tar2[1], player)
      end
    end
  end,
})

hx__qixi:addEffect(fk.EventPhaseStart, {
  anim_type = "masochism",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__qixi.name) then
      if player.phase == Player.Start then
        return player:getMark("@hx__qixi") < 2
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@hx__qixi", 1)
  end,
})

hx__qixi:addEffect(fk.TurnEnd, {
  anim_type = "masochism",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(hx__qixi.name) then
      return player:getMark("@hx__qixi") < 2
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@hx__qixi", 1)
  end,
})

return hx__qixi